package com.bdf.ipharma.gui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import oracle.jdeveloper.layout.GridBagConstraints2;

import com.bdf.ipharma.config.Config;
import com.bdf.ipharma.model.Usuario;
import com.bdf.ipharma.starter.McdError;
import com.bdf.ipharma.starter.McdPrs;

/**
 * A Swing-based top level window class.
 * <P>
 * This screen has a top menu, a base panel and a status bar. The top menu has
 * the option buttons and the title logo. The base panel manages several screens
 * in a cardLayut fashion. The status bar shows status messages depending on
 * displayed screen.
 * </p>
 * 
 * @author JorgE.H
 */
public class GUIMenu extends GUIWindow {

   /**
    * Attributes.
    */
   public boolean dbChanged = false; // indicates weather db has been updated

   public boolean dbRecycleChanged = false; // indicates weather recycle bin

   // has been updated

   public boolean assignment; // indicates weather assignment mode is active

   public boolean doTicket; // indicates weather tickets should be printed



   static final String ACTIVE = "active";

   static final String INACTIVE = "inactive";

   static final String OPERATION = "oper";

   int currOper;

   /**
    * The Layouts.
    */
   GridBagLayout layoutTop = new GridBagLayout();

   GridBagLayout layoutButtons = new GridBagLayout();

   BorderLayout layoutMenu = new BorderLayout();

   CardLayout layoutBase = new CardLayout();

   /**
    * The main panel.
    */
   JPanel jPanelBase = new JPanel();

   JPanel jPanelTop = new JPanel();

   JPanel jPanelButtons = new JPanel();

   JLabel jLblTitle = new JLabel();

   JLabel jLblUser = new JLabel();

   JLabel jLblMsg = new JLabel();

   GUIAssignment guiAssignment = null;

   GUIExtraction guiExtraction = null;

   GUIReturn guiReturn = null;

   GUIRecharge guiRecharge = null;

   GUIRecycle guiRecycle = null;

   GUIButtonImage jButtonExit;

   GUIButtonImage jButtonHelp;

   GUIButtonImage jButt1;

   GUIButtonImage jButt2;

   GUIButtonImage jButt3;

   GUIButtonImage jButt4;

   GUIMenu_jButt1_actionAdapter jButt1Adapter = new GUIMenu_jButt1_actionAdapter(
         this);

   GUIMenu_jButt2_actionAdapter jButt2Adapter = new GUIMenu_jButt2_actionAdapter(
         this);

   GUIMenu_jButt3_actionAdapter jButt3Adapter = new GUIMenu_jButt3_actionAdapter(
         this);

   GUIMenu_jButt4_actionAdapter jButt4Adapter = new GUIMenu_jButt4_actionAdapter(
         this);

   /**
    * Constructs a new instance.
    */
   public GUIMenu(com.bdf.ipharma.starter.McdMain pMain) {

      super(pMain);
      try {
         jbInit();
      } catch (Exception e) {
         McdError.add(e);
      }
   }

 

/**
    * Initializes the state of this instance.
    */
   private void jbInit() throws Exception {

      this.setLayout(layoutMenu);
      this.setBackground(Colors.MNU_BACK);
      this.setRequestFocusEnabled(false);

      // panel: Top
      // buttons
      jButtonHelp = new GUIButtonImage(Images.buttHelp, Images.buttHelpDn);
      jButtonExit = new GUIButtonImage(Images.buttExit, Images.buttExitDn);
      jButtonExit
            .addActionListener(new GUIMenu_jButtonExit_actionAdapter(this));

      // labels
      jLblTitle.setIcon(Images.logo);
      jLblTitle.setHorizontalAlignment(SwingConstants.RIGHT);

      Usuario usuario = theSess.getUsuario();
      jLblUser.setText(usuario.getNombre());//usuario.getApellido() + " " + usuario.getNombre());

      jLblUser.setForeground(Colors.APN_TEXT);
      jLblUser.setFont(Texts.fntUser);
      jLblUser.setHorizontalAlignment(SwingConstants.RIGHT);

      // status/message bar
      jLblMsg.setHorizontalAlignment(SwingConstants.CENTER);
      jLblMsg.setBorder(BorderFactory
            .createMatteBorder(1, 0, 1, 0, Color.black));
      jLblMsg.setBackground(Colors.MNU_STATUS);
      jLblMsg.setForeground(Colors.MNU_STATUS_TEXT);
      jLblMsg.setOpaque(true);
      setStatusMsg(Texts.MNU_TIT_SEL);

      // panel: Option Menu
      jPanelButtons.setLayout(layoutButtons);
      jPanelButtons.setBackground(Colors.MNU_BACK);

      // option buttons
      jButt1 = new GUIButtonImage(Images.butt1, Images.butt1Dn);
      jButt1.setName(INACTIVE);
      jButt1.addActionListener(jButt1Adapter);

      jButt2 = new GUIButtonImage(Images.butt2, Images.butt2Dn);
      jButt2.setName(INACTIVE);
      jButt2.addActionListener(jButt2Adapter);

      jButt3 = new GUIButtonImage(Images.butt3, Images.butt3Dn);
      jButt3.setName(INACTIVE);
      jButt3.addActionListener(jButt3Adapter);

      jButt4 = new GUIButtonImage(Images.butt4, Images.butt4Dn);
      jButt4.setName(INACTIVE);
      jButt4.addActionListener(jButt4Adapter);

      // adds object to menu panel
      jPanelButtons.add(jButt1, new GridBagConstraints2(0, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.NONE,
            Texts.zeroMargin, 10, 0));
      jPanelButtons.add(jButt2, new GridBagConstraints2(1, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.NONE,
            Texts.zeroMargin, 0, 0));
      jPanelButtons.add(jButt3, new GridBagConstraints2(2, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.NONE,
            Texts.zeroMargin, 0, 0));
      jPanelButtons.add(jButt4, new GridBagConstraints2(3, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.NONE,
            Texts.zeroMargin, 0, 0));

      // adds objects to top panel
      jPanelTop.setLayout(layoutTop);
      jPanelTop.setBackground(Colors.MNU_BACK);
      jPanelTop.add(jPanelButtons, new GridBagConstraints2(0, 0, 1, 2, 0.0,
            1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH,
            Texts.zeroMargin, 0, 5));
      // labels
      jPanelTop.add(jLblTitle, new GridBagConstraints2(1, 0, 1, 1, 0.5, 0.0,
            GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(5, 0,
                  0, 5), 0, 0));
      jPanelTop.add(jLblUser, new GridBagConstraints2(1, 1, 1, 1, 0.5, 0.0,
            GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0,
                  2, 5), 0, 0));
      // button: Help
      jPanelTop.add(jButtonHelp, new GridBagConstraints2(2, 0, 1, 2, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.NONE, Texts.zeroMargin,
            0, 0));
      // button: Exit
      jPanelTop.add(jButtonExit, new GridBagConstraints2(3, 0, 1, 2, 0.0, 0.0,
            GridBagConstraints.EAST, GridBagConstraints.NONE, Texts.zeroMargin,
            0, 0));
      jPanelTop.add(jLblMsg, new GridBagConstraints2(0, 2, 4, 1, 1.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            Texts.zeroMargin, 0, 0));

      // adds objects to this panel
      this.add(jPanelTop, BorderLayout.NORTH);
      this.add(jPanelBase, BorderLayout.CENTER);
   }

   /**
    * Disables unavailable buttons
    */
   public void disableButtons() {

      // disables unavailable options
     

   }

   /**
    * Highlights current option button
    * 
    * @param pCurrOp
    *           current operation number
    */
   public void toggleButtons(int pCurrOp) {

      // toggles all buttons up
      jButt1.setIcon(Images.butt1);
      jButt1.setName(INACTIVE);
      jButt2.setIcon(Images.butt2);
      jButt2.setName(INACTIVE);
      jButt3.setIcon(Images.butt3);
      jButt3.setName(INACTIVE);
      jButt4.setIcon(Images.butt4);
      jButt4.setName(INACTIVE);
      // toggles current button down
      switch (pCurrOp) {
      case McdPrs.extractId:
         jButt1.setIcon(Images.butt1Dn);
         jButt1.setName(ACTIVE);
         break;
      case McdPrs.returnId:
         jButt2.setIcon(Images.butt2Dn);
         jButt2.setName(ACTIVE);
         break;
      case McdPrs.recycleId:
         jButt3.setIcon(Images.butt3Dn);
         jButt3.setName(ACTIVE);
         break;
      case McdPrs.rechargeId:
         jButt4.setIcon(Images.butt4Dn);
         jButt4.setName(ACTIVE);
         break;
      case McdPrs.assignmentId:
         jButt1.setIcon(Images.butt1Dn);
         jButt1.setName(ACTIVE);
         break;
      }

   }

   /**
    * Starts execution
    */
   public void start() {

      Config config = (Config) theSess.get(McdPrs.CONFIG);
      jPanelBase.setLayout(layoutBase);

      try {
    	  if (assignment) {
               guiAssignment = new GUIAssignment(this);
               jPanelBase.add(guiAssignment, OPERATION
                     + guiAssignment.getType());
            }
            //guiExtraction = new GUIExtraction(this);
            // adds objects to panelBase
            //jPanelBase.add(guiExtraction, OPERATION + guiExtraction.getType());
//         }
 
        /*  
         if (usuarioService.isValidFunction(usuario, McdPrs.ftnReturn)) {
            guiReturn = new GUIReturn(this);
            // adds object to panelBase
            jPanelBase.add(guiReturn, OPERATION + guiReturn.getType());
         }
         if (usuarioService.isValidFunction(usuario, McdPrs.ftnRecharge)) {
            guiRecharge = new GUIRecharge(this);
            // adds object to panelBase
            jPanelBase.add(guiRecharge, OPERATION + guiRecharge.getType());
         }
         if (usuarioService.isValidFunction(usuario, McdPrs.ftnRecycle)) {
            guiRecycle = new GUIRecycle(this);
            // adds object to panelBase
            jPanelBase.add(guiRecycle, OPERATION + guiRecycle.getType());
         }
         // turns unavailable buttons off
         disableButtons();

         // show first screen (this is required OTSK)
         layoutBase.first(jPanelBase);

         this.setVisible(true);
         this.startTimer();

         // if operation requires assignment, starts with assignment panel
         if (usuarioService.isValidFunction(usuario, McdPrs.ftnExtract)) {
            if (assignment)
               showPanel(guiAssignment);
            else
               showPanel(guiExtraction);
         }
      */
      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }

   /**
    * Switches to the extraction screen passing the assignment value
    * 
    * @param pAssign
    *           the selected assignment
    */


   /**
    * Switches to the assignment screen canceling the assignment
    * 
    * @param pAssign
    *           the selected assignment
    */


   /**
    * Sets status bar message
    * 
    * @param pMsg
    *           the new message string
    */
   public void setStatusMsg(String pMsg) {

      jLblMsg.setText("> " + pMsg + " <");
   }

   /**
    * Changes the active screen
    * 
    * @param pOper
    *           the operation object to set current
    */
   void showPanel(GUIOperation pOper) {

   

   }

   // BOTON EXTRACCION
   void jButt1_actionPerformed(ActionEvent e) {

 

   }

   // BOTON DEVOLUCION
   void jButt2_actionPerformed(ActionEvent e) {

      // performs action only if button is not highlighted
      if (!jButt2.getName().equals(ACTIVE))
         showPanel(guiReturn);
   }

   // BOTON VACIAR BUZON
   void jButt3_actionPerformed(ActionEvent e) {

      // performs action only if button is not highlighted
      if (!jButt3.getName().equals(ACTIVE))
         showPanel(guiRecycle);
   }

   // BOTON REPOSICION
   void jButt4_actionPerformed(ActionEvent e) {

      // performs action only if button is not highlighted
      if (!jButt4.getName().equals(ACTIVE))
         showPanel(guiRecharge);
   }

   void jButtonExit_actionPerformed(ActionEvent e) {

    
      this.stop();
   }

}

class GUIMenu_jButt1_actionAdapter implements java.awt.event.ActionListener {

   GUIMenu adaptee;

   GUIMenu_jButt1_actionAdapter(GUIMenu adaptee) {

      this.adaptee = adaptee;
   }

   public void actionPerformed(ActionEvent e) {

      adaptee.jButt1_actionPerformed(e);
   }
}

class GUIMenu_jButt2_actionAdapter implements java.awt.event.ActionListener {

   GUIMenu adaptee;

   GUIMenu_jButt2_actionAdapter(GUIMenu adaptee) {

      this.adaptee = adaptee;
   }

   public void actionPerformed(ActionEvent e) {

      adaptee.jButt2_actionPerformed(e);
   }
}

class GUIMenu_jButt3_actionAdapter implements java.awt.event.ActionListener {

   GUIMenu adaptee;

   GUIMenu_jButt3_actionAdapter(GUIMenu adaptee) {

      this.adaptee = adaptee;
   }

   public void actionPerformed(ActionEvent e) {

      adaptee.jButt3_actionPerformed(e);
   }
}

class GUIMenu_jButt4_actionAdapter implements java.awt.event.ActionListener {

   GUIMenu adaptee;

   GUIMenu_jButt4_actionAdapter(GUIMenu adaptee) {

      this.adaptee = adaptee;
   }

   public void actionPerformed(ActionEvent e) {

      adaptee.jButt4_actionPerformed(e);
   }
}

class GUIMenu_jButtonExit_actionAdapter implements
      java.awt.event.ActionListener {

   GUIMenu adaptee;

   GUIMenu_jButtonExit_actionAdapter(GUIMenu adaptee) {

      this.adaptee = adaptee;
   }

   public void actionPerformed(ActionEvent e) {

      adaptee.jButtonExit_actionPerformed(e);
   }
}
